#include "msg_api.h"
#include "slave.h"
#include "master.h"
#include "../base/mq.h"

size_t _msgapi_send_msg( struct message* msg, const char* dest , struct slave_api* sapi ) {
	return _msgapi_send_message_by_slavename(_slave_api_ref_master(sapi),msg,_slave_name( _slave_api_ref_slave(sapi) ) , dest);
}

size_t _msgapi_send_message_by_slavename( struct master* m ,struct message* msg,const char* src, const char* dest ) {
	struct slave* dest_slave = _master_find_slave(m,dest);
	if( dest_slave != NULL ) {
		msg->sid = _slave_new_sid(dest_slave);
		msg->src = src;
		msgqueue_enqueue(_slave_ref_msgqueue(dest_slave),msg);
		return msg->sid;
	}
	return SLAVE_RESERVE_SID_VALUE;
}